package cn.xjy.ddlc.context;

import cn.xjy.ddlc.model.Table;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.ToString;

import java.util.List;

@EqualsAndHashCode(callSuper = true)
@Data
@ToString(callSuper = true)
public class MatchedTableItem extends AbstractMatchedItem {
    
    /**
     * 左边表
     */
    Table left;
    
    /**
     * 右边表
     */
    Table right;
    
    List<MatchedColumnItem> columns;
    
    /***
     * 索引
     */
    List<MatchedIndexItem> indexes;
    
    private CompareResult result;
    
    @Override
    public CompareResult compare() {
        if (left == null) {
            result = CompareResult.LEFT_NOT_EXIST;
            return result;
        }
        if (right == null) {
            result = CompareResult.RIGHT_NOT_EXIST;
            return result;
        }
        result = CompareResult.EQUAL;
        
        for (MatchedColumnItem columnItem : columns) {
            if (CompareResult.EQUAL != columnItem.compare()) {
                result = CompareResult.NOT_EQUAL;
            }
        }
        
        for (MatchedIndexItem indexItem : indexes) {
            if (CompareResult.EQUAL != indexItem.compare()) {
                result = CompareResult.NOT_EQUAL;
            }
        }
        return result;
    }
    
}
